/*
 *  Copyright 2010-2011 Mark Eschbach
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 * $HeadURL$
 * $Id$
 */
package com.meschbach.psi.tomcat;

import com.meschbach.cise.util.StreamCopier;
import com.meschbach.cise.vfs.DeleteFileTree;
import com.meschbach.psi.CompositePSIException;
import com.meschbach.psi.Container;
import com.meschbach.psi.PSIException;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.catalina.Context;
import org.apache.catalina.Host;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.startup.Embedded;

/**
 *
 * This class is copyright 2010-2011 by Mark Eschbach and is licensed under the
 * Apache License, Version 2.0; accessible at
 * http://www.apache.org/licenses/LICENSE-2.0.<p>
 *
 * @author "Mark Eschbach" &lt;meschbach@gmail.com&gt;
 */
public class TomcatContainer implements Container {

    File virtualRoot;
    String baseURL;
    Embedded embedded;
    Host host;

    public TomcatContainer(File virtualRoot, String baseURL, Embedded embedded, Host host) {
        this.virtualRoot = virtualRoot;
        this.baseURL = baseURL;
        this.embedded = embedded;
        this.host = host;
    }

    public void deploy(String aContext, InputStream aWebApplicationArchive) throws PSIException {
        /*
         * Pull through the WAR through
         */
        File file = new File(virtualRoot, aContext + ".war");
        try {
            /*
             * Copy the data
             */
            FileOutputStream fos = new FileOutputStream(file);
            try {
                StreamCopier.copy(aWebApplicationArchive, fos);
            } finally {
                fos.close();
            }
        } catch (IOException ioe) {
            throw new PSIException(ioe);
        }
        /*
         * Deploy the artifact
         */
        Context ctx = embedded.createContext(aContext, file.getAbsolutePath());
        host.addChild(ctx);
    }

    public String getRemoteURL() {
        return baseURL;
    }

    public void shutdown() throws PSIException {
        CompositePSIException cpe = new CompositePSIException();
        /*
         * Shutdown the container
         */
        try {
            embedded.stop();
        } catch (LifecycleException ex) {
            cpe.addException(new PSIException("Unable to stop the embedded container", ex));
        }
        /*
         * Clean up the temporary directory
         */
        try {
            DeleteFileTree.delete(virtualRoot);
        } catch (IOException ioe) {
            cpe.addException(new PSIException("Unable to clean up temprorary folder '" + virtualRoot.getAbsolutePath() + "'", ioe));
        }
        /*
         * If we failed to clean up, then throw
         */
        cpe.throwIfApplicable();
    }
}
